
/**
 房间运营状态
 *
*/

import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { UtilsHelper } from "utils/position/UtilsHelper";
 

/**
 * 战斗状态 - 赶路状态
 * 抵达房目标房间后则为战斗状态
 * 在抵达房间之前为赶路状态
 * 用来更新治疗和攻击指令
*/
export class TeamAttackState<T extends IStateArg<ATeamCombat,ITask>> extends State<T>
{
    
     
    public getActions(arg:T):Action[]
    {
        const entity = arg.entity;
        // global.Log.Info(`${entity.GUID} asssert 2 ${arg.entity.getCreepCountAlive()}`);
        if(entity.creeps.some(e=>e.hits<e.hitsMax))
        {
            entity.healTeam();
        }
        else 
        {
            entity.healAroundCreep();
        }
       
        const targets =entity.getNodeMemory().targets;
        // 1.抵达目标房间后更新攻击目标
        if(targets)
        {
            // global.Log.Info(`${entity.GUID} asssert 3 ${arg.entity.getCreepCountAlive()}`);
            // 2.攻击
            if(targets.length>0 && Game.rooms[targets[0].attackTarget.roomName])
            {
                // 有视野
                // 2.1初始化攻击的目标
                if( !targets[0].target)
                {
                    for(let idx=0;idx<   targets.length;)
                    {
                        const target = UtilsHelper.findAttackTarget(targets[idx].attackTarget);
                        if(target.length == 0)
                        {
                            targets.splice(idx,1);
                        }
                        else
                        {
                            targets[idx].target = target[0];
                            idx++
                        }
                    }
                }
                // global.Log.Info(`${entity.GUID} asssert 4 ${arg.entity.getCreepCountAlive()}`);
                const attacktargets:string[]=[];
                for(let i=0;i<targets.length;)
                {
                    const targetid = targets[i].target;
                    if(!targetid)
                    {
                        targets.splice(i,1);
                    }
                    else
                    {
                        if(Game.getObjectById(targetid))
                        {
                            attacktargets.push(targets[i].target as string);
                            i++;
                        }
                        else
                        {
                            targets.splice(i,1);
                        }

                    }
                }
                // toptask.move?.position = entity.attack(attacktargets);
                entity.attack(attacktargets);
            }
            else
            {
                // global.Log.Info(`${entity.GUID} asssert 5 ${arg.entity.getCreepCountAlive()}`);
                entity.attack([])
            }
            if(!targets || targets.length == 0)
            {
                // 没有攻击目标。
                // global.Log.Info(`${entity.GUID} asssert 6 ${arg.entity.getCreepCountAlive()}`);
                entity.getNodeMemory().targets=[];
            }

        }
        return [];
    }

}
